package cn.ibizlab.odoo.core.odoo_purchase.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union;
import cn.ibizlab.odoo.core.odoo_purchase.domain.Purchase_bill_union;
import cn.ibizlab.odoo.core.odoo_purchase.filter.Purchase_bill_unionSearchContext;
import cn.ibizlab.odoo.core.odoo_purchase.service.IPurchase_bill_unionService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[采购 & 账单] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Purchase_bill_unionServiceImpl implements IPurchase_bill_unionService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ipurchase_bill_unionClientService purchase_bill_unionClientService;

    @Override
    public boolean create(Purchase_bill_union purchase_bill_union) {
        boolean bOk = false ;
        Ipurchase_bill_union clientModel = this.convert2Model(purchase_bill_union,null);
        purchase_bill_unionClientService.create(clientModel) ;
        this.convert2Domain(clientModel,purchase_bill_union);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Purchase_bill_union> purchase_bill_unions, int batchSize) {

        //purchase_bill_unionClientService.updateBatch(purchase_bill_unions,batchSize) ;

    }

    @Override
    public Purchase_bill_union get(Integer id) {
        Ipurchase_bill_union clientModel = purchase_bill_unionClientService.createModel();
        clientModel.setId(id);
        purchase_bill_unionClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Purchase_bill_union get(Purchase_bill_union purchase_bill_union) {
        Purchase_bill_union retpurchase_bill_union = this.get(purchase_bill_union.getId()) ;
    	BeanCopier copier=BeanCopier.create(retpurchase_bill_union.getClass(), purchase_bill_union.getClass(), false);
        copier.copy(retpurchase_bill_union, purchase_bill_union, null);
        return purchase_bill_union ;
    }

    @Override
    public void createBatch(List<Purchase_bill_union> purchase_bill_unions, int batchSize) {

                        //purchase_bill_unionClientService.createBatch(purchase_bill_unions,batchSize) ;

    }

    @Override
    public boolean update(Purchase_bill_union purchase_bill_union) {
        boolean bOk = false ;
        Ipurchase_bill_union clientModel = this.convert2Model(purchase_bill_union,null);
        purchase_bill_unionClientService.update(clientModel) ;
        this.convert2Domain(clientModel,purchase_bill_union) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //purchase_bill_unionClientService.removeBatch(purchase_bill_unions) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ipurchase_bill_union clientModel = purchase_bill_unionClientService.createModel();
        clientModel.setId(id);
        purchase_bill_unionClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Purchase_bill_union> searchDefault(Purchase_bill_unionSearchContext context) {
        List<Purchase_bill_union> list = new ArrayList<Purchase_bill_union>();
        Page<cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union> clientModelList = purchase_bill_unionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Purchase_bill_union>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据发票查询
     */
    @Override
    public Page<Purchase_bill_union> selectByOdoo_vendor_bill(Integer id){
        Purchase_bill_unionSearchContext context = new Purchase_bill_unionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Purchase_bill_union> list = new ArrayList<Purchase_bill_union>();
        Page<cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union> clientModelList = purchase_bill_unionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Purchase_bill_union>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据采购订单查询
     */
    @Override
    public Page<Purchase_bill_union> selectByOdoo_purchase_order(Integer id){
        Purchase_bill_unionSearchContext context = new Purchase_bill_unionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Purchase_bill_union> list = new ArrayList<Purchase_bill_union>();
        Page<cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union> clientModelList = purchase_bill_unionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Purchase_bill_union>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Purchase_bill_union> selectByOdoo_company(Integer id){
        Purchase_bill_unionSearchContext context = new Purchase_bill_unionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Purchase_bill_union> list = new ArrayList<Purchase_bill_union>();
        Page<cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union> clientModelList = purchase_bill_unionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Purchase_bill_union>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据币种查询
     */
    @Override
    public Page<Purchase_bill_union> selectByOdoo_currency(Integer id){
        Purchase_bill_unionSearchContext context = new Purchase_bill_unionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Purchase_bill_union> list = new ArrayList<Purchase_bill_union>();
        Page<cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union> clientModelList = purchase_bill_unionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Purchase_bill_union>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Purchase_bill_union> selectByOdoo_partner(Integer id){
        Purchase_bill_unionSearchContext context = new Purchase_bill_unionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Purchase_bill_union> list = new ArrayList<Purchase_bill_union>();
        Page<cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union> clientModelList = purchase_bill_unionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipurchase_bill_union clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Purchase_bill_union>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ipurchase_bill_union convert2Model(Purchase_bill_union domain , Ipurchase_bill_union model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = purchase_bill_unionClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getReferenceDirtyFlag())
            model.setReference(domain.getReference());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getAmountDirtyFlag())
            model.setAmount(domain.getAmount());
         if(domain.getDateDirtyFlag())
            model.setDate(domain.getDate());
         if(domain.getPurchase_order_id_textDirtyFlag())
            model.setPurchase_order_id_text(domain.getPurchase_order_id_text());
         if(domain.getCurrency_id_textDirtyFlag())
            model.setCurrency_id_text(domain.getCurrency_id_text());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getVendor_bill_id_textDirtyFlag())
            model.setVendor_bill_id_text(domain.getVendor_bill_id_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getVendor_bill_idDirtyFlag())
            model.setVendor_bill_id(domain.getVendor_bill_id());
         if(domain.getCurrency_idDirtyFlag())
            model.setCurrency_id(domain.getCurrency_id());
         if(domain.getPurchase_order_idDirtyFlag())
            model.setPurchase_order_id(domain.getPurchase_order_id());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
        return model ;
    }

    public Purchase_bill_union convert2Domain( Ipurchase_bill_union model ,Purchase_bill_union domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Purchase_bill_union();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getReferenceDirtyFlag())
            domain.setReference(model.getReference());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getAmountDirtyFlag())
            domain.setAmount(model.getAmount());
        if(model.getDateDirtyFlag())
            domain.setDate(model.getDate());
        if(model.getPurchase_order_id_textDirtyFlag())
            domain.setPurchase_order_id_text(model.getPurchase_order_id_text());
        if(model.getCurrency_id_textDirtyFlag())
            domain.setCurrency_id_text(model.getCurrency_id_text());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getVendor_bill_id_textDirtyFlag())
            domain.setVendor_bill_id_text(model.getVendor_bill_id_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getVendor_bill_idDirtyFlag())
            domain.setVendor_bill_id(model.getVendor_bill_id());
        if(model.getCurrency_idDirtyFlag())
            domain.setCurrency_id(model.getCurrency_id());
        if(model.getPurchase_order_idDirtyFlag())
            domain.setPurchase_order_id(model.getPurchase_order_id());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        return domain ;
    }


}
